package com.fiberhome.nas.feature.main.viewmodel

import android.content.Context
import androidx.lifecycle.viewModelScope
import com.fiberhome.nas.core.common.base.viewmodel.BaseViewModel
import com.fiberhome.nas.core.data.state.AppState
import com.fiberhome.nas.feature.main.model.SharedStateContainer
import com.fiberhome.nas.feature.main.model.TabDestination
import com.fiberhome.nas.navigation.AppNavigator
import com.fiberhome.nas.navigation.routes.FileRoutes
import com.fiberhome.nas.navigation.routes.PhotoRoutes
import com.fiberhome.nas.navigation.routes.SettingRoutes
import com.fiberhome.nas.navigation.routes.TaskCenterRoutes
import dagger.hilt.android.lifecycle.HiltViewModel
import dagger.hilt.android.qualifiers.ApplicationContext
import kotlinx.coroutines.flow.SharingStarted
import kotlinx.coroutines.flow.stateIn
import kotlinx.coroutines.launch
import javax.inject.Inject

/**
 * 主页面ViewModel
 */
@HiltViewModel
class MainViewModel @Inject constructor(
    appState: AppState,
    navigator: AppNavigator,
    private val shareState: SharedStateContainer,
    @ApplicationContext private val context: Context
) : BaseViewModel(navigator, appState) {

    /**
     * 编辑模式状态
     */
    val editMode = shareState.switchEditMode.stateIn(
        scope = viewModelScope,
        started = SharingStarted.WhileSubscribed(5000),
        initialValue = false
    )

    /**
     * 退出编辑模式
     */
    fun exitEditMode() {
        viewModelScope.launch { shareState.switchEditMode(false) }
    }

    /**
     * 上传文件
     */
    fun navigateUpload(index: Int) {
        when (index) {
            TabDestination.PHOTO.index -> {
                super.toPage(PhotoRoutes.UPLOAD)
            }

            TabDestination.ALBUM.index -> {
                //TODO 新建相册
            }

            TabDestination.FILE.index -> {
                super.toPage(FileRoutes.UPLOAD)
            }

            else -> throw IllegalStateException("此页面不支持上传功能")

        }
    }

    /**
     * 跳转到任务中心
     */
    fun navigateTaskCenter() {
        super.toPage(TaskCenterRoutes.TASK_CENTER_ROUTE)
    }

    /**
     * 跳转到设置
     */
    fun navigateSetting() {
        super.toPage(SettingRoutes.SETTING_ROUTE)
    }


}

